#!/bin/sh
#Heo Run Wizard

title="Heo Run Wizard"

problems=("OneMax" "Generic" "GridMin" "MaxSat" "NFAMin" "ODE1IVP" "QAP" "Rastrigin")

#set -x

binary_dir="Release"
#binary_dir="Debug"

#predefined vars (uncomment to skip dialog):

#problem_choice="1"
#instance_name="100"
#method_choice="1 2 3"
#num_threads="1 2 4 8"
#rand_seeds="3293 4546"
#tech_choice="1 2"
#mpi_time_limit="10"
#mpi_options="0"

#NOTE: 'dialog' is a more or less standard utility for building console-based front ends.
#(tarball can be obtained from http://hightek.org/dialog/)

to_lower()
{
	echo $1 | tr "[:upper:]" "[:lower:]"
}

no_quote()
{
	echo $1 | tr -d "\""
}

dialog_result()
{
	case $1 in
  	0)
		result=`cat $tempfile`
		if [ -z "$result" ]
		then
			exit 255
		fi
		echo "$result";;
	esac
	exit $1
}

dialog_result_z()
{
	case $1 in
  	0)
		cat $tempfile;;
	esac
	exit $1
}

make_menu()
{
        local i
        local j
        local count
	let count=${#argument1[*]}
	for ((i=0; i<$count; i++))
	do
 		let j=i+1
		echo "$j ${argument1[i]}"
	done
}

#try to obtain temp file
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/heo_temp_file$$

#kill temp file at exit
trap "rm -f $tempfile" 0 1 2 5 15

if [ -z "$problem_choice" ]
then
#	echo "dialog 1"

	argument1=( "${problems[@]}" )
	array_list=`make_menu`

	dialog --title "$title" --menu "Select problem:" 20 61 ${#problems[*]} $array_list 2> $tempfile

	problem_choice=`dialog_result $?` || exit $?
fi

if [ -z "$instance_name" ]
then
#	echo "dialog 2"

	dialog --title "$title" --inputbox "Enter problem instance name:" 20 61 2> $tempfile

	instance_name=`dialog_result $?` || exit $?
fi

method_name=(ga sa ga_sa)

if [ -z "$method_choice" ]
then
#	echo "dialog 3"

	dialog --title "$title" --checklist "Select optimization methods:" 20 61 \
3 1 "GA" ON \
  2 "SA" ON \
  3 "GA+SA" ON \
2> $tempfile

	method_choice=`dialog_result $?` || exit $?
fi

if [ -z "$num_threads" ]
then
#	echo "dialog 4"

	dialog --title "$title" --inputbox "Enter number of threads:" 20 61 "1 2 4 8" 2> $tempfile

	num_threads=`dialog_result $?` || exit $?
fi

if [ -z "$rand_seeds" ]
then
#	echo "dialog 5"

	dialog --title "$title" --inputbox "Enter rand seeds (one for each independent run):" 20 61 "3293 4546" 2> $tempfile

	rand_seeds=`dialog_result $?` || exit $?
fi

tech_name1=(omp mpi mpi2)
mpi_run=("mpirun" "mpiexec")

if [ -z "$tech_choice" ]
then
#	echo "dialog 6"

	dialog --title "$title" --checklist "Select parallel technologies:" 20 61 \
3 1 "OpenMP" ON \
  2 "MPI 1.x (mpirun)" ON \
  3 "MPI 2.x (mpiexec)" off \
2> $tempfile

	tech_choice=`dialog_result $?` || exit $?
fi

if [ "$tech_choice" != '"1"' ]
then

if [ -z "$mpi_time_limit" ]
then
#	echo "dialog 7"

	dialog --title "$title" --inputbox "Enter mpi job timeout (minutes, 0=none):" 20 61 "0" 2> $tempfile

	mpi_time_limit=`dialog_result $?` || exit $?
fi

mpi_opt_name=("-pwdfile ../../../logins/logins.txt" "-machinefile ../../../logins/hosts.txt" \
	"-localonly" "-channel shm")

if [ -z "$mpi_options" ]
then
#	echo "dialog 8"

	dialog --title "$title" --checklist "Select MPI options:" 20 61 \
4 1 "-pwdfile ../../../logins/logins.txt" off \
  2 "-machinefile ../../../logins/hosts.txt" off \
  3 "-localonly" off \
  4 "-channel shm" off \
2> $tempfile

	mpi_options=`dialog_result_z $?` || exit $?
fi

fi
#if [ "$dummy" != "" ]
#then
#fi

problem=${problems[$problem_choice-1]}
problem=`to_lower $problem`

echo "problem:     '$problem'"
echo "instance:    '$instance_name'"
echo "method:      '$method_choice'"
echo "threads:     '$num_threads'"
echo "rand seeds:  '$rand_seeds'"
echo "technology:  '$tech_choice'"
echo "mpi timeout: '$mpi_time_limit'"
echo "mpi options: '$mpi_options'"

#current working directory
work_dir=~+

run_dir="$work_dir/$problem/run"
mkdir -p "$run_dir"

sh_name_list=""

create_sh()
{
	local i
	local j
	local tmp
	local method
	local mpi_args
	tmp=`no_quote $1`
	let i=tmp
	let i=i-1
	tmp=`no_quote $2`
	let j=tmp
	let j=j-1
#	echo $i $j
	method="${method_name[$i]}"
	sh_name="${run_dir}/${method}_${tech_name1[$j]}.sh"
	echo -e \
"#!/bin/sh\n\
num_threads=\"${num_threads}\"\n\
rand_seeds=\"${rand_seeds}\"\n\
for j in \${rand_seeds}\n\
do\n\
	mkdir -p ../res/${instance_name}/\${j}\n\
	for i in \${num_threads}\n\
	do\n\
		echo randseed:\${j}, cpu:\${i}\
" > $sh_name

	if ((j==0))
	then
		echo -e \
"		export OMP_NUM_THREADS=\${i}\n\
		../${method}/omp/${binary_dir}/${problem}_${method}_omp \$j ${method}_${instance_name}.xml \
> ../res/${instance_name}/\${j}/\${i}.${method}.omp.txt\
" >> $sh_name
	else
		mpi_args=""
		if [ "$mpi_time_limit" != "0" ] && [ "$mpi_time_limit" != "" ]
		then
			mpi_args="-timeout $mpi_time_limit "
		fi

		for q in $mpi_options
		do
			mpi_args="$mpi_args${mpi_opt_name[$q-1]} "
		done

		echo -e \
"		${mpi_run[$j-1]} -np \${i} -stdout ../res/${instance_name}/\${j}/\${i}.${method}.mpi.txt $mpi_args\
../${method}/mpi/${binary_dir}/${problem}_${method}_mpi \$j ${method}_${instance_name}.xml\
" >> $sh_name
	fi

	echo -e \
"	done\n\
done\n\
" >> $sh_name
	chmod +x $sh_name
	sh_name_list="${sh_name_list}\n${sh_name}"
}

for s in $method_choice
do
	for t in $tech_choice
	do
		create_sh $s $t
	done
done

if [ "$sh_name_list" != "" ]
then
	echo -e "sh file(s) created successfully: $sh_name_list"
fi

#eof
